package ace.module.oauth2.server.sdk.core.feign.impl;

import ace.module.oauth2.server.api.constant.Oauth2ServerConstant;
import ace.module.oauth2.server.sdk.core.constant.SdkConstant;
import feign.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author caspar
 * @date 2023/3/3 13:34
 */
@FeignClient(
    name = "${spring.cloud.openfeign.client.oauth2AuthFeign.name:" + SdkConstant.FEIGN_SERVICE_NAME + "}",
    contextId = "oauth2AuthFeign"
)
public interface Oauth2AuthFeign {

  @PostMapping(Oauth2ServerConstant.OAUTH2_TOKEN_ENDPOINT_URI)
  @Operation(summary = "使用rest认证类型,并根据restGrantType类型进行授权登录")
  Response getTokenByRestGrantType(
      @Parameter(name = HttpHeaders.AUTHORIZATION, description = "认证方式")
      @RequestHeader(HttpHeaders.AUTHORIZATION)
      String authorization,
      @Parameter(description = "认证类型业务参数,json格式") @RequestBody Object body,
      @Parameter(name = "grantType", description = "认证类型") @RequestParam(value = "grant_type")
      String grantType,
      @Parameter(name = "restGrantType", description = "rest认证类型")
      @RequestParam(value = "rest_grant_type")
      String restGrantType);

  @PostMapping(Oauth2ServerConstant.OAUTH2_TOKEN_ENDPOINT_URI)
  @Operation(summary = "使用指定认证类型,进行授权登录")
  Response getTokenByGrantType(
      @Parameter(name = HttpHeaders.AUTHORIZATION, description = "认证方式")
      @Valid
      @NotEmpty
      @RequestHeader(HttpHeaders.AUTHORIZATION)
      String authorization,

      @Parameter(name = "grantType", description = "认证类型")
      @Valid
      @NotEmpty
      @RequestParam(value = "grant_type")
      String grantType,

      @Parameter(description = "认证类型业务参数,json格式")
      @Valid
      @NotNull
      @RequestBody
      Object body);
}
